package com.wis.bolt.mpg;

import com.wis.TopologyDrive;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Description: traffic_wis    将相同要素的收集到一起
 * @author: fan
 * @Date: Created in 2018/12/17 10:47
 * @Modified By:
 */
public class MPGShouJiBolt extends BaseRichBolt {
    private OutputCollector collector;
    private static Queue tuplePpQueue = new ConcurrentLinkedQueue();
    private static Queue tupleVbQueue = new ConcurrentLinkedQueue();
    private static Queue tupleWsQueue = new ConcurrentLinkedQueue();
    private static Queue tupleAtQueue = new ConcurrentLinkedQueue();
    private static Queue tupleRiQueue = new ConcurrentLinkedQueue();
    private static Queue tupleXtQueue = new ConcurrentLinkedQueue();
    private static Queue tupleIdQueue = new ConcurrentLinkedQueue();
    private static Queue tupleRhQueue = new ConcurrentLinkedQueue();
    private static String typeProduce;

    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        typeProduce = map.get(TopologyDrive.TYPE_PRODUCE).toString();
    }

    @Override
    public void execute(Tuple tuple) {
        String key = tuple.getStringByField("key");
        String njd = tuple.getStringByField("nengjiandu");

        try {
            if (StringUtils.isNotBlank(key)) {
                // 判断是否是一个切片的结束标识位
                if (njd.equals("over")) {
                    if (tupleVbQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "2" + key + "00", StringUtils.join(tupleVbQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleWsQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "3" + key + "00", StringUtils.join(tupleWsQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tuplePpQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "1" + key + "00", StringUtils.join(tuplePpQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleAtQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "5" + key + "00", StringUtils.join(tupleAtQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleRiQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "4" + key + "00", StringUtils.join(tupleRiQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleRhQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "6" + key + "00", StringUtils.join(tupleRhQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));

                    tuplePpQueue.clear();
                    tupleWsQueue.clear();
                    tupleVbQueue.clear();
                    tupleAtQueue.clear();
                    tupleRiQueue.clear();
                    tupleIdQueue.clear();
                    tupleXtQueue.clear();
                    tupleRhQueue.clear();
                    // 判断是否是一个数据文件的结束标识位
                } else if (njd.equals("jies")) {
                    if (tupleVbQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "2" + key + "00", StringUtils.join(tupleVbQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "2" + key + "00", "jies", "jies", "jies"));

                    if (tupleWsQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "3" + key + "00", StringUtils.join(tupleWsQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "3" + key + "00", "jies", "jies", "jies"));

                    if (tuplePpQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "1" + key + "00", StringUtils.join(tuplePpQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "1" + key + "00", "jies", "jies", "jies"));

                    if (tupleAtQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "5" + key + "00", StringUtils.join(tupleAtQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "5" + key + "00", "jies", "jies", "jies"));

                    if (tupleRiQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "4" + key + "00", StringUtils.join(tupleRiQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "4" + key + "00", "jies", "jies", "jies"));
                    if (tupleRhQueue.size() > 1)
                        this.collector.emit(new Values("0" + typeProduce + "6" + key + "00", StringUtils.join(tupleRhQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("0" + typeProduce + "6" + key + "00", "jies", "jies", "jies"));
                    tuplePpQueue.clear();
                    tupleWsQueue.clear();
                    tupleVbQueue.clear();
                    tupleAtQueue.clear();
                    tupleRiQueue.clear();
                    tupleIdQueue.clear();
                    tupleXtQueue.clear();
                    tupleRhQueue.clear();
                } else {
                    // TODO 这儿的字段为null，好像没必要去做判断
                    String id = tuple.getStringByField("id");
                    if (StringUtils.isNotBlank(id)) {
                        tupleIdQueue.add(id);
                        tupleVbQueue.add(njd);
                        tupleWsQueue.add(tuple.getStringByField("fengsu"));
                        tuplePpQueue.add(tuple.getStringByField("jiangshuiliang"));
                        tupleAtQueue.add(tuple.getStringByField("qiwen"));
                        tupleRiQueue.add(tuple.getStringByField("daolujiebing"));
                        tupleXtQueue.add(tuple.getStringByField("jiangshuixiangtai"));
                        tupleRhQueue.add(tuple.getStringByField("xiangduishidu"));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.collector.ack(tuple);
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("key", "yaosu", "id", "xt"));
    }
}
